package com.example.neusoft215gdh.androidpro_21.Fragement;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.neusoft215gdh.androidpro_21.Activity.AddpostActivity;
import com.example.neusoft215gdh.androidpro_21.Adapter.ShaituAdapter;
import com.example.neusoft215gdh.androidpro_21.JavaBean.Shaitu;
import com.example.neusoft215gdh.androidpro_21.R;
import com.example.neusoft215gdh.androidpro_21.Service.ShaituSeverice;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShaiTuFragment extends BaseFragment {

	private RecyclerView gridView;
	private ShaituAdapter adapter;
	private  String shaitu="shaitu";
	private ImageView dialog;

	public ShaiTuFragment() {
		// Required empty public constructor
	}


	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		gridView = (RecyclerView) view.findViewById(R.id.gridview);
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl("http://172.24.10.175/workout/api.php/")
				.addConverterFactory(GsonConverterFactory.create())
				.build();

		ShaituSeverice shaituSeverice =retrofit.create(ShaituSeverice.class);
		Call<List<Shaitu>> Listcall=shaituSeverice.shaituList("g54acvhfs9doq6u6o0aepo18q7",shaitu,1);

		Listcall.enqueue(new Callback<List<Shaitu>>() {
			@Override
			public void onResponse(Call<List<Shaitu>> call, Response<List<Shaitu>> response) {
				List<Shaitu> shaituList = response.body();

				if (getActivity() != null) {
					adapter = new ShaituAdapter(getActivity(),shaituList);
					LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
					layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

					/**
					 * 布局显示两列
					 */
					gridView.setLayoutManager(new StaggeredGridLayoutManager(2,
							StaggeredGridLayoutManager.VERTICAL));
					gridView.setAdapter(adapter);
					gridView.setItemAnimator(new DefaultItemAnimator());

				}

			}

			@Override
			public void onFailure(Call<List<Shaitu>> call, Throwable t) {

			}
		});

	ImageView	imageView5 = (ImageView)view.findViewById(R.id.imageView5);
		imageView5.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent();
				intent.setClass(getActivity(),AddpostActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.fragment_post, container, false);
	}

}